<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
		<link rel="stylesheet" href="js/bower_components/bootstrap/dist/css/bootstrap.min.css">
		<link rel="stylesheet" href="js/bower_components/bootstrap-select/dist/css/bootstrap-select.min.css">
		<link rel="stylesheet" href="js/bower_components/bootstrap-switch/dist/css/bootstrap3/bootstrap-switch.min.css">
		<link rel="stylesheet" href="js/bower_components/bootstrap-table/dist/bootstrap-table.min.css">
		<link rel="stylesheet" href="js/bower_components/bootstrap-treeview/dist/bootstrap-treeview.min.css">
		<link href="js/bower_components/bootstrap-datetimepicker-master/css/bootstrap-datetimepicker.min.css" rel="stylesheet">
		
		<script src="js/bower_components/jquery/dist/jquery.min.js"></script>
		<script src="js/bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
		<script src="js/bower_components/bootbox.min.js"></script>
		<script src="js/bower_components/bootstrap-select/js/bootstrap-select.js"></script>
		<script src="js/bower_components/bootstrap-switch/dist/js/bootstrap-switch.min.js"></script>
		<script src="js/bower_components/bootstrap-table/dist/bootstrap-table.min.js"></script>
		<script src="js/bower_components/bootstrap-table/dist/locale/bootstrap-table-zh-CN.js"></script>
		
		<script src="js/bower_components/bootstrap-treeview/dist/bootstrap-treeview.min.js"></script>
		<script src="js/bower_components/jquery-form-validator/form-validator/jquery.form-validator.min.js"></script>
		<script src="js/bower_components/moment/min/moment-with-locales.min.js"></script>
		<script src="js/bower_components/bootstrap-datetimepicker-master/js/bootstrap-datetimepicker.min.js"></script>
		<script>
			function getData(){
				MyIp={
					"beginPage":0,
					"pageCount":5
				};
				$.ajax({
					type:"put",
					url:"http://localhost:8080/myIp/getIpByTime",
					contentType:"application/json;charset=utf-8",
					data:JSON.stringify(MyIp),
					dataType:"json",
					success:function(res){
						console.log("获取分页后的数据，返回结果是：");
						console.log(res);
						setTabel(res)
					}
				});
			}
			
			function setTabel(myIpList){
				$("#tbMyIp").bootstrapTable('destroy')
				$("#tbMyIp").bootstrapTable({
					data:myIpList,
					striped:true,
					contentType:"aaplication/x-www-form-urlencoded;charset=utf-8",
					uniqueId:"value",
					idFieId:"userid",
					checkboxHeader:true,
					singleSelect:true,
					clickToSelect:true,
					columns:[{
						field:"userid",
						title:"id",
						align:"center",
						visible:true
						},{
						field:"ip",
						align:"center",
						title:"ip"
						},{
						field:"opname",
						align:"center",
						title:"操作名称"
						},{
						field:"optime",
						align:"center",
						title:"操作时间"
						
						}
					]
				})
			}
			
			
			
			$(function(){
				getData()
				
				function getSelectID(){
					var id =$("#tbMyIp").bootstrapTable('getSelections');
					console.log(id);
					if(id.length>0){
						return id[0].userid;
					}else{
						bootbox.alert("必须选择一条要修改的信息!")
					}
				}
				
				$("#btnAdd").click(function(){
					$("#myMobdelLable").text("新增用户信息")
					$("#divMyIpForm").modal();
				})
				
				$("#btnSave").click(function(){
					var id=$("#userid").val();
					console.log(id);
					if(id ==null){
						MyIp={
							"ip":$("#ip").val(),
							"opname":$("#opname").val(),
							"userid":id
						};
						$.ajax({
							type:"put",
							url:"http://localhost:8080/myIp/updateIp",
							contentType:"application/json;charset=utf-8",
							data:JSON.stringify(MyIp),
							dataType:"json",
							success:function(res){
								console.log("修改数据后，返回结果是：");
								console.log(res);
								if(res>0){
									alert("保存成功");
									$("#divMyIpForm").modal('hide')
									getData();
								}
							}
						});
					}else{
						MyIp={
							
							"ip":$("#ip").val(),
							"opname":$("#opname").val(),
							"userid":id
						};
						$.ajax({
							type:"put",
							url:"http://localhost:8080/myIp/addIp",
							contentType:"application/json;charset=utf-8",
							data:JSON.stringify(MyIp),
							dataType:"json",
							success:function(res){
								console.log("添加数据后，返回结果是：");
								console.log(res);
								if(res>0){
									alert("保存成功");
									$("#divMyIpForm").modal('hide')
									getData();
								}
							}
						});
					}
					})
					
				$("btnUpdate").click(function(){
					var userid = getSelectID();
					console.log(userid);
					$.ajax({
						type:"get",
						url:"http://localhost:8080/myIp/getIpByUserid?userid="+userid,
						contentType:"application/json;charset=utf-8",
						dataType:"json",
						success:function(res){
							console.log("根据主键获取后端，返回结果是：");
							console.log(res);
							if(res!=null){
								$("#myMobdelLable").Text("修改信息")
								$("#ip").val(res["ip"]);
								$("#opname").val(res["opname"]);
								$("#userid").val(res["userid"]);
								$("#divMyIpForm").modal();
							}
						}
					});
				})
				
				$("btnDel").click(function(){
					console.log("进入删除方法");
					var userid = getSelectID();
					console.log(userid);
					$.ajax({
						type:"get",
						url:"http://localhost:8080/myIp/deleteIp?userid="+userid,
						contentType:"application/json;charset=utf-8",
						dataType:"json",
						success:function(res){
							console.log("根据主键删除数据，返回结果是：");
							console.log(res);
							if(res==1){
								bootbox.alert("删除成功!");
							}
						}
					});
				})
				
				$("#btnSelect").click(function(){
					var username=$("#txtIpname").val();
					console.log(username);
					if(username.length==0){
						getData()
					}else{
						MyIp={
							"beginPage":0,
							"pageCount":4,
							"username":username
						};
						$.ajax({
							type:"put",
							url:"http://localhost:8080/myIp/getAll",
							contentType:"application/json;charset=utf-8",
							data:JSON.stringify(MyIp),
							dataType:"json",
							success:function(res){
								console.log("获取分页后的数据，返回结果是：");
								console.log(res);
								setTabel(res)
							}
						});
					}
				})
			})
		</script>
	</head>
	<body>
		<div class="input-group">
			用户名称：<input type="text" id="txtIpname" />&nbsp;&nbsp;
			<button type="button" id="btnSelect" class="btn btn-default">搜索</button>
		</div>
		<div style="margin: 5px;margin-top: 10px;">
			<button type="button" id="btnAdd" class="btn btn-success">新增</button>
			<button type="button" id="btnUpdate" class="btn btn-info">修改</button>
			<button type="button" id="btnDel" class="btn btn-danger">删除</button>
		</div>
		<div class="row col-md-10" style="margin-top: 10px;">
			<table id="tbMyIp"></table>
		</div>
		<div class="modal fade" id="divMyIpForm" tabindex="-1" role="dialog" aria-labelledby="MyModel" aria-hidden="true">
			<div class="modal-dialog" style="width: 400px;">
				<div class="modal-content">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal" aria-hidder="true">&times;</button>
						<h4 class="modal-title" id="myMobdelLable">用户信息</h4>
	<!-- 					<input type="hidden" id="userid" /> -->
					</div>
					<div class="modal-body form-inline">
						<div class="form-group">
							<label class="control-label" for="userid">用户&nbsp;&nbsp;&nbsp;&nbsp;id：</label>
							<input style="width: 220px;" class="form-control" type="text" id="userid" />
						</div>
						<br />
						<div class="form-group">
							<label class="control-label" for="opname">操作名称：</label>
							<input style="width: 220px;" class="form-control" type="text" id="opname" />
						</div>
						<br />
						<div class="form-group">
							<label class="control-label" for="ip">ii&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pp：</label>
							<input style="width: 220px;" class="form-control" type="text" id="ip" required="required" />
						</div>
						<br />
						<br />
						<div class="modal-footer">
							<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
							<button type="button" class="btn btn-primary" id="btnSave">保存</button>
						</div>
					</div>
				</div>
			</div>
		</div>
	</body>
</html>
